<?php
//
// 响应用户消息
// 微信公众账号响应给用户的不同消息类型
//

define("TOKEN", "weixin");

$wechatObj = new wechatCallbackapiTest();
if (!isset($_GET['echostr'])) {
    $wechatObj->responseMsg();
}else{
    $wechatObj->valid();
}

class wechatCallbackapiTest
{

    public function valid()
    {
        $echoStr = $_GET["echostr"];
        if ($this->checkSignature()) {
            echo $echoStr;
            exit;
        }
    }

    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);

        if ($tmpStr == $signature) {
            return true;
        } else {
            return false;
        }
    }


    /**
    * 第二节课内容
     */
    public function responseMsg()
    {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        if (!empty($postStr)) {
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $RX_TYPE = trim($postObj->MsgType);

            //用户发送的消息类型判断
            switch ($RX_TYPE) {
                case "text":
                    $result = $this->transmitText($postObj, $this->dealWithText($postObj) );
                    break;
                case "image":
                    $result = $this->transmitText($postObj,"这个是一个图片");
                    break;
                case "voice":
                    $result = $this->transmitText($postObj, $this->dealWith($postObj));
                    break;
                case "video":
                    $result = $this->transmitText($postObj, "这个是一个视频");
                    break;
                case "event":
                    $content = array();
                    $content[] = array("Title"=>"多图文1标题", "Description"=>"描述", "PicUrl"=>"http://avatar.csdn.net/0/0/8/1_javazejian.jpg", "Url" =>"http://blog.forvoid.me/");
                    $content[] = array("Title"=>"多图文2标题", "Description"=>"描述", "PicUrl"=>"http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg", "Url" =>"http://blog.csdn.net/canot/article/details/52495333");
                    $content[] = array("Title"=>"多图文3标题", "Description"=>"描述", "PicUrl"=>"http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg", "Url" =>"http://blog.csdn.net/canot/article/details/52495333");
                    $result = $this->transmitNews($postObj, $content);
//                    $result = $this->dealwithEvent($postObj);
                    break;
                default:
                    $result = "unknow msg type: " . $RX_TYPE;
                    break;
            }
            echo $result;
        } else {
            echo "";
            exit;
        }
    }
    public function dealwithEvent($postObj){

    }
    //回复图文消息
    private function transmitNews($object, $newsArray)
    {
        if(!is_array($newsArray)){
            return;
        }
        $itemTpl = "    <item>
        <Title><![CDATA[%s]]></Title>
        <Description><![CDATA[%s]]></Description>
        <PicUrl><![CDATA[%s]]></PicUrl>
        <Url><![CDATA[%s]]></Url>
    </item>
";
        $item_str = "";
        foreach ($newsArray as $item){
            $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
        }
        $newsTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<Content><![CDATA[]]></Content>
<ArticleCount>%s</ArticleCount>
<Articles>
$item_str</Articles>
</xml>";

        $result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray));
        return $result;
    }
    /**
    *文字识别
     */
    public function dealWithText($postObj){
        $keyword = trim($postObj->Content);
//        $result = "";
        if (strpos($keyword,"Hello")!== false ||strpos($keyword,"你好")!== false ){
            $result = "你好！很高兴为你服务";
        }else
            if (strpos($keyword,"学校")!== false ){
                $result = "重庆邮电大学";
            }else{
                $result = "这是一个文本文件   你发送的内容为:".$keyword;
            }
        return $result;
    }
    /**
    *语音自动回复
     */
    public function dealWith($postObj){
        $keyword = trim($postObj->Recognition);
//        $result = "";
        if (strpos($keyword,"Hello")!== false ||strpos($keyword,"你好")!== false ){
            $result = "你好！很高兴为你服务";
        }else
            if (strpos($keyword,"学校")!== false ){
                $result = "重庆邮电大学";
            }else{
                $result = $keyword;
            }
        return $result;
    }

    /*
     * 回复文本消息
     */
    private function transmitText($object, $content)
    {
        $textTpl = "<xml>
            <ToUserName><![CDATA[%s]]></ToUserName>
            <FromUserName><![CDATA[%s]]></FromUserName>
            <CreateTime>%s</CreateTime>
            <MsgType><![CDATA[text]]></MsgType>
            <Content><![CDATA[%s]]></Content>
            </xml>";
        $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
        return $result;
    }

}

?>